package com.szhuang.mobilesafe.activity;

import android.app.Activity;
import android.os.Bundle;
import android.os.Vibrator;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.EditText;
import android.widget.Toast;

import com.szhuang.mobilesafe.R;
import com.szhuang.mobilesafe.db.dao.NumberAddressQueryUtils;

/**
 * Created by szhuang on 2015/3/27.
 */
public class NumberAddressQueryActivity extends Activity {

    private EditText edit_phone,result;
    private Vibrator vibrator;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_number_address_query);

        init();

        edit_phone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                if(s!=null&&s.length()>2){
                    String address=NumberAddressQueryUtils.queryNumber(s.toString().trim());
                    result.setText(address);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    private void init() {
        edit_phone=(EditText)findViewById(R.id.edit_phone);
        result=(EditText)findViewById(R.id.result);
        vibrator= (Vibrator) getSystemService(VIBRATOR_SERVICE);
    }

    public void queryAddress(View view){
        String phone=edit_phone.getText().toString().trim();
        if(TextUtils.isEmpty(phone)){
            Toast.makeText(this,"号码不能为空！",Toast.LENGTH_LONG).show();
            Animation shake= AnimationUtils.loadAnimation(this,R.anim.shake);
            edit_phone.startAnimation(shake);
            vibrator.vibrate(1000);
        }else{

            String address= NumberAddressQueryUtils.queryNumber(phone);
            result.setText(address);
        }
    }
}
